package com.sv.utils.ibatis;

import java.sql.Connection;
import java.sql.SQLException;

import com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback;
import com.ibatis.sqlmap.engine.scope.StatementScope;

public class SqlExecutor extends com.ibatis.sqlmap.engine.execution.SqlExecutor {

    private Dialect dialect = new MysqlDialect();
    
    public boolean enableLimit = true;
    
    public void executeQuery(StatementScope statementScope, Connection conn, String sql, Object[] parameters, int skipResults, int maxResults, RowHandlerCallback callback) throws SQLException {
        
            if ((skipResults != NO_SKIPPED_RESULTS || maxResults != NO_MAXIMUM_RESULTS)
                    && supportsLimit()) {
                    sql = dialect.getLimitSql(sql, skipResults, maxResults);
                    //设置skipResults、maxResults让下面执行的方法不分页，分页已迁移至sql中物理分页
                    skipResults = NO_SKIPPED_RESULTS;
                    maxResults = NO_MAXIMUM_RESULTS;
                    }
        super.executeQuery(statementScope, conn, sql, parameters, skipResults, maxResults, callback);

      }


      /**
       * Execute a stored procedure
       *
       * @param statementScope     - the request scope
       * @param conn        - the database connection
       * @param sql         - the sql to call the procedure
       * @param parameters  - the parameters for the procedure
       * @param skipResults - the number of results to skip
       * @param maxResults  - the maximum number of results to return
       * @param callback    - a row handler for processing the results
       * @throws SQLException - if the procedure fails
       */
      public void executeQueryProcedure(StatementScope statementScope, Connection conn, String sql, Object[] parameters, int skipResults, int maxResults, RowHandlerCallback callback) throws SQLException {
          if ((skipResults != NO_SKIPPED_RESULTS || maxResults != NO_MAXIMUM_RESULTS)
                  && supportsLimit()) {
                  sql = dialect.getLimitSql(sql, skipResults, maxResults);
//                  if(logger.isDebugEnabled()){
//                  logger.debug(sql);
//                  }
                  System.out.println(sql);
                  skipResults = NO_SKIPPED_RESULTS;
                  maxResults = NO_MAXIMUM_RESULTS;
                  }
          super.executeQueryProcedure(statementScope, conn, sql, parameters, skipResults, maxResults, callback);
          
      }

      
      private boolean supportsLimit(){
          if(enableLimit && null != dialect){
              return true;
          }
          return false;
      }

    public Dialect getDialect() {
        return dialect;
    }

    public void setDialect(Dialect dialect) {
        this.dialect = dialect;
    }

    public boolean isEnableLimit() {
        return enableLimit;
    }

    public void setEnableLimit(boolean enableLimit) {
        this.enableLimit = enableLimit;
    }
      
      
}
